class Solution
{
public:
    bool isItPossible(string word1, string word2)
    {
        vector<int> word1CharCount(26, 0);
        vector<int> word2CharCount(26, 0);
        for (char c : word1)
        {
            ++word1CharCount[c - 'a'];
        }
        for (char c : word2)
        {
            ++word2CharCount[c - 'a'];
        }
        auto positive = [](int num)
        {
            return num > 0;
        };
        // 直接暴力就好了。比赛的时候试图写巨复杂的逻辑判断浪费了一些时间
        for (int i = 0; i < 26; ++i)
        {
            for (int j = 0; j < 26; ++j)
            {
                if (positive(word1CharCount[i] && positive(word2CharCount[j])))
                {
                    --word1CharCount[i];
                    ++word2CharCount[i];
                    --word2CharCount[j];
                    ++word1CharCount[j];
                    if (count_if(word1CharCount.begin(), word1CharCount.end(), positive) == count_if(word2CharCount.begin(), word2CharCount.end(), positive))
                    {
                        return true;
                    }
                    ++word1CharCount[i];
                    --word2CharCount[i];
                    ++word2CharCount[j];
                    --word1CharCount[j];
                }
            }
        }
        return false;
    }
};